fix(parser): keep last occurrence of streaming message id within file (#110)#155
fix(parser): keep last occurrence of streaming message id within file (#110)#155ousamabenyounes wants to merge 2 commits intogetagentseal:mainfrom
Conversation
…getagentseal#110) Claude Code streams an assistant response across several JSONL writes that share the same `message.id`: an early `message_start` (empty content), optional mid- stream updates, and a final `message_stop` carrying the `tool_use` blocks plus authoritative usage. `groupIntoTurns` deduplicates by id keeping the FIRST occurrence, so `tool_use` (MCP, Agent, EnterPlanMode, …) and final token counts in the last entry were silently dropped. Add a within-file pre-pass `dedupeStreamingMessageIds` in `parseSessionFile` that keeps the LAST occurrence of each message id. Cross-file dedup against `seenMsgIds` in `groupIntoTurns` stays keep-first-seen (it serves a different purpose: avoiding double-counting when the same session appears under several project dirs). Adds `tests/parser-streaming-dedup.test.ts` covering streaming dedup, mixed user/assistant entries, no-id passthrough, and ordering between distinct ids. Co-Authored-By: Ora Studio <[email protected]>
|
Hi, dedupeStreamingMessageIds keeps only the last occurrence of each message.id, which makes parseApiCall/groupIntoTurns use the later streaming update’s timestamp (typically message_stop) as the call timestamp. This can incorrectly include/exclude turns in dateRange filtering and shift day bucketing near midnight because the code explicitly uses the first assistant call timestamp as the cost-incurrence time. Severity: action required | Category: correctness How to fix: Preserve first timestamp per id Agent prompt to fix - you can give this to your LLM of choice:
Found by Qodo code review |
…tagentseal#155 review) Qodo bot flagged that keeping the LAST occurrence of a streaming message id changed the assistant call timestamp to the later `message_stop` time, breaking date-range bucketing and filtering in `parseSessionFile` (which treats the cost-incurrence timestamp as the first assistant call time). Fix: in dedupeStreamingMessageIds, track both first-seen and last-seen indices per id. Keep the LAST entry's content (tool_use blocks, authoritative usage) but override its `timestamp` with the FIRST entry's value, so downstream date logic remains stable. Tests updated to assert both invariants and a new test verifies the input array is not mutated.
|
Good catch on the timestamp semantics. Pushed Tests updated to assert both invariants; added a non-mutation test on the input array. Full suite: 360 / 360. |
|
Closing this. The streaming dedup fix itself is solid, but the PR also silently removes flushCodexCache() and flushAntigravityCache() calls from parseProviderSources, which would break cache persistence for codex and antigravity users (every run re-parses everything from scratch). These removals aren't mentioned in the PR description. We'll apply the dedup fix separately. Thanks for identifying the streaming issue though. |
Summary
Fixes #110.
Claude Code writes the same
message.idmultiple times to the session JSONL as a response streams in. Only the final write carries thetool_useblocks (MCP servers, Agent, EnterPlanMode, …) and the authoritative token counts. The existing dedup ingroupIntoTurnskeeps the first occurrence by id, so for every streamed turn:tool_useblocks for Agent / EnterPlanMode / Bash are droppedFix
Add a within-file pre-pass
dedupeStreamingMessageIdsinparseSessionFilethat keeps the last occurrence of eachmessage.id. The cross-file dedup ingroupIntoTurns(againstseenMsgIds) is correct for what it does — avoiding double-counting when the same session appears under multiple project dirs — and stays keep-first-seen.The two concerns are now handled separately:
parseSessionFile(new)groupIntoTurns(unchanged)Verification
tests/parser-streaming-dedup.test.tsfails on the unfixed code (verified by stashing the fix and re-running)npx tsc --noEmitclean{}-init maps insrc/parser.tsorsrc/providers/(semgrep guard)Files changed
src/parser.tsdedupeStreamingMessageIds; called fromparseSessionFilebeforegroupIntoTurnstests/parser-streaming-dedup.test.tsVibe Coded by Ousama Ben Younes
Developed With Ora Studio (Claude Code)